<?php
namespace app\admin\controller;
use app\admin\Controller\Common;
use app\admin\model\AuthGroup as AuthGroupModel;
use app\admin\model\AuthRule as AuthRuleModel;
use think\Db;
/*
 * -- ----------------------------
 * --  用户组管理
 * -- ----------------------------
 */
class AuthGroup extends Common
{
  /**
   * 用户组添加
   */
  public function add(){
    if(request()->isAjax()){
      $data = input('post.');
      if(!isset($data['status'])){
        $data['status'] = 0;
      }
      if(isset($data['rules'])){
        $data['rules']=implode(',',$data['rules']);
      }else{
        $data['rules']='';
      }
      $AuthGroupModel = new  AuthGroupModel();
      if($AuthGroupModel->save($data)){
        $back = [
          'statu' =>200,
          'info'  =>"添加成功"
        ];
      }else{
        $back = [
          'statu' =>400,
          'info'  =>"添加成功"
        ];
      }
      return $back;
    }
    $AuthRule = new AuthRuleModel();
    $AuthRuleModel = $AuthRule->AuthRuleTree();
    $this->assign('AuthRule',$AuthRuleModel);
    return view();
  }

  /** 
   * 用户组列表
   */
  public function lst(){
    $AuthGroupModel = new  AuthGroupModel();
    $AuthGroup = AuthGroupModel::paginate(10);
    $this->assign('AuthGroup',$AuthGroup);
    return view();
  }

  /** 
   * 用户组修改
   */
  public function edit(){
    if(request()->isPost()){
      $data = input('post.');
      if(!isset($data['status'])){
        $data['status'] = 0;
      }
      if(isset($data['rules'])){
        $data['rules']=implode(',',$data['rules']);
      }else{
        $data['rules']='';
      }
      $code = db('Auth_group')->where('id',input('id'))->update($data);
      if($code){
        $back = [
          'statu' =>200,
          'info'  =>"修改成功"
        ];
      }else{
        $back = [
          'statu' =>200,
          'info'  =>"修改成功"
        ];
      }
      return $back;
    }
    $AuthRule = new AuthRuleModel();
    $AuthRuleModel = $AuthRule->AuthRuleTree();
    
    $AuthGroup = db('Auth_group')->find(input('id'));
    $this->assign(array(
      'AuthGroup'=>$AuthGroup,
      'AuthRule'=>$AuthRuleModel
    ));
    return view();
  }

  /** 
   * 用户组删除
   */
  public function del(){
    $id = input('id');
    $AuthGroup = db('auth_group_access')->where('group_id',$id)->field('uid')->select();
    if(!empty($AuthGroup)){
      foreach($AuthGroup as $k => $v){
        $groupuid[] = $v['uid'];
      }
      //开启事务
      Db::startTrans();
      $c = db('Auth_group')->where('id',$id)->delete();
      $a = Db::name("admin")->delete($groupuid);
      $b = Db::name("auth_group_access")->where('group_id',$id)->delete();
      if($a && $b && $c){
        //提交事物
        Db::commit();
        $back = [
          'statu' =>200,
          'info'  =>'删除成功'
        ];
      }else{
        //回滚事务
        Db::rollback();
        $back = [
          'statu' =>400,
          'info'  =>'删除失败'
        ];
      }
    }else{
      $res = db('Auth_group')->where('id',$id)->delete();
      if($res){
        $back = [
          'statu' =>200,
          'info'  =>'删除成功'
        ];
      }else{
        $back = [
          'statu' =>400,
          'info'  =>'删除失败'
        ];
      }
    }
    return $back;
  }

}
?>